package com.angel.activity;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import com.angel.adapter.WilAdapter;
import com.angel.entity.Task;
import com.angel.entity.WillBean;
import com.angel.entity.WillBeanList;
import com.angel.port.IActivity;
import com.angel.util.BaseUtil;
import com.angel.util.NetUtil;
import com.angel.util.UserInfoUtil;
import com.google.gson.reflect.TypeToken;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

/** 意愿清单 */
public class WilingMenuActivity extends Activity implements IActivity{
	/** 自定义Adapter */
	private WilAdapter wilAdapter = null;
	private ListView listView = null;
	private MyOnItemClickListener listener;
	private Context context;
	private String willId;//意愿Id
	private WillBeanList willBeanList;
	private List<WillBean> willList;
	private HashMap<String,String> map=new HashMap<String,String>();
	private ProgressDialog progressDialog;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wilingmenu_activity);
		 MobclickAgent.onError(this);//自动捕获异常退出（FC ）
		context=WilingMenuActivity.this;
		progressDialog=new ProgressDialog(context);
		progressDialog.setMessage("正在加载数据,请稍后...");
		progressDialog.show();
		listView = (ListView) findViewById(R.id.wil_ListView);
		//启动后台，请求数据
		map.put("userId",UserInfoUtil.getValue(context, "userId", ""));
		Log.i("userId","WillMenu-->userId"+UserInfoUtil.getValue(context, "userId", ""));
		BaseUtil.startService(Task.WILL_ACTIVITY,WilingMenuActivity.this, map);
	}
	class MyOnItemClickListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long id) {
			    willList=willBeanList.getResult();
				willId=String.valueOf(willList.get(position).getId());
				Log.i("WilingMenuActivity","WilingMenuActivity-->willId"+willId);
				Intent tent=new Intent(WilingMenuActivity.this,WillingMenuEdit.class);
				tent.putExtra("willId", willId);//传递willId
				tent.putExtra("describe", willList.get(position).getWillContent());
		   	    startActivity(tent);
				Toast.makeText(WilingMenuActivity.this, "will"+willId, Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public void init() {
	}
	@Override
	public void refresh(Object... params) {
		 String isSucess=BaseUtil.getValueFromJSON((String)params[0]);
		
		 if(isSucess.equals("2")){
			 BaseUtil.makeToast(WilingMenuActivity.this, "对不起,网络连接超时,请稍后重试...");
			 progressDialog.dismiss();
			 return ;
		}
		
		Type type=new TypeToken<WillBeanList>(){}.getType();
		willBeanList=(WillBeanList)BaseUtil.getEntityFromJson((String)params[0], type);
		progressDialog.dismiss();//
		
		willList=willBeanList.getResult();
		wilAdapter = new WilAdapter(context,willList);
		listView.setAdapter(wilAdapter);
		listener =new MyOnItemClickListener();
		listView.setOnItemClickListener(listener);
	}
	/*点击添加按钮*/
	public  void setTingWill(View v){
		if(NetUtil.checkNetState(context)){
			MobclickAgent.onEvent(this, "addWill");//发送点击添加意愿统计到友盟
			boolean b=BaseUtil.isBindPhone(context);
			if(b){
				Intent tent=new Intent(WilingMenuActivity.this,WillingMenuEdit.class);
				tent.putExtra("willId", "0");
		   	    startActivity(tent);
			}else{//绑定手机号
				Intent tent=new Intent(WilingMenuActivity.this,Register1Activity.class);
				tent.putExtra("isBind", "1");//表示手机号绑定
				startActivity(tent);
			}
		}else{
			BaseUtil.makeToast(this, "请检查您的网络连接");
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			BaseUtil.showExitDialog(this);
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		 MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		 MobclickAgent.onPause(this);
	}
}
